<?php
include('../../include/functions.inc.php');
include('../../include/conversor.php');
require_once('../../include/tcpdf/tcpdf.php');

$strSerie = getParam("SERIE");
$strNumero = getParam("NUMERO");
$strFecha = date("d") . '                    ' . date("M") . '                   ' . date("Y");
$sinTotal = 0;

$f = $db->query_select("cllg_factura", "SERIE= " . sqlValue($strSerie, "text") . " AND NUMERO=" . sqlValue($strNumero, "int"));
$h = $f[0];
$asign = array();
$fullname = "";
$career = "";
if ($h["FK_STUDENT"] != NULL) {
    $stud = $asign = $db->queryToArray("select a.FIRST_NAME,a.SECOND_NAME,a.LAST_NAME from cllg_student a where a.ID=" . $h["FK_STUDENT"]);
    $fullname = $stud[0]["FIRST_NAME"] . " " . $stud[0]["SECOND_NAME"] . " " . $stud[0]["LAST_NAME"];
    $asign = $db->query_toArray("select a.FK_CAREER,b.NAME from cllg_student_assign a,cllg_career b where a.FK_CAREER=b.ID AND a.FK_STUDENT=" . $h["FK_STUDENT"]);
    $career = $asign[0]["NAME"];
}
$d = $db->query_select("cllg_factura_detalle", "FK_FACTURA=" . sqlValue($h["ID"], "int"));



$pdf = new TCPDF("", "mm", "LETTER", true, 'UTF-8', false);
$pdf->setPageOrientation("");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(0, 0, 0);

// set font
$pdf->SetFont('helveticaB', '', 10);

// add a page
$pdf->AddPage();

// set cell padding
$pdf->setCellPaddings(0, 0, 0, 0);

// set cell margins
$pdf->setCellMargins(0, 0, 0, 0);

// set color for background
$pdf->SetFillColor(0, 0, 0, 0);



//Serie y Numero 
$pdf->MultiCell(20, 1, $strSerie . ' - ' . $strNumero, 0, 'L', false, 1, 125, 6, true);
//Fecha
$pdf->MultiCell(60, 1, $strFecha, 0, 'L', 0, 1, 100, 21, true);
//Nombre
$pdf->MultiCell(130, 1, $h["NOMBRE"], 0, 'L', 0, 1, 24, 32, true);
//Direccion
$pdf->MultiCell(125, 1, $h["DIRECCION"], 0, 'L', 0, 1, 24, 38, true);
//NIT
$pdf->MultiCell(35, 1, $h["NIT"], 0, 'L', 0, 1, 130, 43, true);

//Detalle
$intY = 60;
foreach ($d as $i) {
    $pdf->MultiCell(90, 1, $i["DESCRIPCION"], 0, 'L', 0, 1, 15, $intY, true);
    $pdf->MultiCell(20, 1, number_format($i["SUBTOTAL"], 2), 0, 'R', 0, 1, 127, $intY, true);
    $intY += 4;
    $sinTotal += $i["SUBTOTAL"];
}
$pdf->MultiCell(120, 2, $fullname, 0, 'C', 0, 1, 20, 86, true);
$pdf->MultiCell(120, 2, $career, 0, 'C', 0, 1, 20, 91, true);

$pdf->MultiCell(90, 2, ucfirst(PrecioEnLetras($sinTotal)), 0, 'L', 0, 1, 24, 96, true);
$pdf->MultiCell(20, 1, number_format($sinTotal, 2), 0, 'R', 0, 1, 127, 96, true);

$pdf->Output('Factura.pdf', 'I');
?>
